﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using EmaMetadata;
using System.Windows.Forms;
using System.Data;

namespace EmaEditor
{
    public static class ComboBoxHelper
    {
        #region Fact helper methods
        /// <summary>
        /// Extension method to retrieve a value from a combo box
        /// </summary>
        /// <param name="ddlPlatform"></param>
        /// <returns></returns>
        public static EmaFact GetFact(this ComboBox cbx)
        {
            DataTable dt = cbx.DataSource as DataTable; 
            if (dt != null) {
                if (cbx.SelectedIndex >= 0 && cbx.SelectedIndex < dt.Rows.Count) {
                    DataRow dr = dt.Rows[cbx.SelectedIndex];
                    EmaFact f = new EmaFact();
                    f.ID = int.Parse(dr["ID"].ToString());
                    f.FactName = (string)dr["Value"];

                    // Do we have a namespace column at all?
                    if (dt.Columns.Contains("NS")) {
                        f.NS = (string)dr["NS"];
                    } else {
                        f.NS = Factory.ENTMERCH_NAMESPACE;
                    }
                    return f;
                }
            }
            return null;
        }

        /// <summary>
        /// Extension method to retrieve a value from a combo box
        /// </summary>
        /// <param name="ddlPlatform"></param>
        /// <returns></returns>
        public static void SetFact(this ComboBox cbx, EmaFact fact)
        {
            if (fact == null) {
                SetFact(cbx, null, null, null);
            } else {
                SetFact(cbx, fact.NS, fact.ID.ToString(), fact.FactName);
            }
        }

        /// <summary>
        /// Use all the appropriate values from a fact without having an actual fact object
        /// </summary>
        /// <param name="cbx"></param>
        /// <param name="NS"></param>
        /// <param name="ID"></param>
        /// <param name="Name"></param>
        public static void SetFact(this ComboBox cbx, string NS, string ID, string Name)
        {
            DataTable dt = cbx.DataSource as DataTable;
            if (dt != null) {
                for (int i = 0; i < dt.Rows.Count; i++) {
                    if (String.Equals(dt.Rows[i]["ID"].ToString(), ID) && String.Equals(dt.Rows[i]["NS"], NS)) {
                        cbx.SelectedIndex = i;
                        return;
                    }
                }
            }
            cbx.SelectedIndex = 0;
        }

        /// <summary>
        /// Setup a drop down box with the
        /// </summary>
        /// <param name="cbx"></param>
        /// <param name="FactGroup"></param>
        public static void SetupList(this ComboBox cbx, string FactGroup)
        {
            List<LookupItem> list = Factory.GetItems(FactGroup);

            // Generate a data table with the necessary members
            DataTable dt = new DataTable();
            dt.Columns.Add("ID");
            dt.Columns.Add("NS");
            dt.Columns.Add("Value");

            // Add a blank selection option for the first value
            DataRow dr = dt.NewRow();
            dr["ID"] = 0;
            dr["NS"] = "";
            dr["Value"] = "(select)";
            dt.Rows.Add(dr);

            // Add all the real values
            if (list != null) {
                foreach (LookupItem li in list) {
                    dr = dt.NewRow();
                    dr["ID"] = li.ID;
                    dr["NS"] = li.NS;
                    dr["Value"] = li.FactName;
                    dt.Rows.Add(dr);
                }
            }

            // Add a "Insert New" selection option for the first value
            dr = dt.NewRow();
            dr["ID"] = -1;
            dr["NS"] = "";
            dr["Value"] = "(insert new user defined item)";
            dt.Rows.Add(dr);

            // Add the list to the dropdown
            cbx.DataSource = dt;
            cbx.DisplayMember = "Value"; // C# knows this refers to the Value column in dt, same for id
            cbx.ValueMember = "ID";
            cbx.Tag = FactGroup;

            // Hook an event to launch the "user defined item" combo box
            cbx.SelectedIndexChanged += new EventHandler(cbx_SelectedIndexChanged);
        }

        static void cbx_SelectedIndexChanged(object sender, EventArgs e)
        {
            ComboBox cbx = sender as ComboBox;
            int val = Convert.ToInt32(cbx.SelectedValue);
            if (val == -1) {
                frmUserDefinedItem dlg = new frmUserDefinedItem((string)cbx.Tag);
                if (dlg.ShowDialog() == DialogResult.OK) {
                    cbx.SelectedIndexChanged -= cbx_SelectedIndexChanged;
                    cbx.SetupList((string)cbx.Tag);   
                }
            }
        }
        #endregion

        #region Lookup code methods
        /// <summary>
        /// Extension method to retrieve a value from a combo box
        /// </summary>
        /// <param name="ddlPlatform"></param>
        /// <returns></returns>
        public static string GetCode(this ComboBox cbx)
        {
            DataTable dt = cbx.DataSource as DataTable;
            if (dt != null) {
                if (cbx.SelectedIndex >= 0 && cbx.SelectedIndex < dt.Rows.Count) {
                    return dt.Rows[cbx.SelectedIndex]["ID"].ToString();
                }
            }
            return null;
        }

        /// <summary>
        /// Extension method to retrieve a value from a combo box
        /// </summary>
        /// <param name="ddlPlatform"></param>
        /// <returns></returns>
        public static void SetCode(this ComboBox cbx, string code)
        {
            if (code == null) {
                cbx.SelectedIndex = 0;
            } else {
                DataTable dt = cbx.DataSource as DataTable;
                if (dt != null) {
                    for (int i = 0; i < dt.Rows.Count; i++) {
                        if (String.Equals(dt.Rows[i]["ID"].ToString(), code)) {
                            cbx.SelectedIndex = i;
                            return;
                        }
                    }
                }
            }
        }

        public static void SetupList(this ComboBox cbx, Dictionary<string, string> dict)
        {
            // Generate a data table with the necessary members
            DataTable dt = new DataTable();
            dt.Columns.Add("ID");
            dt.Columns.Add("Value");

            // Add a blank selection option for the first value
            DataRow dr = dt.NewRow();
            dr["ID"] = 0;
            dr["Value"] = "(select)";
            dt.Rows.Add(dr);

            // Add all the real values
            if (dict != null) {
                foreach (KeyValuePair<string, string> kvp in dict) {
                    dr = dt.NewRow();
                    dr["ID"] = kvp.Key;
                    dr["Value"] = kvp.Value + " (" + kvp.Key + ")";
                    dt.Rows.Add(dr);
                }
            }

            // Add the list to the dropdown
            cbx.DataSource = dt;
            cbx.DisplayMember = "Value"; // C# knows this refers to the Value column in dt, same for id
            cbx.ValueMember = "ID";
        }
        #endregion
    }
}
